package com.buka.recipe.security.point;

import com.alibaba.fastjson2.JSON;
import com.buka.recipe.common.result.Result;
import com.buka.recipe.common.result.ResultCode;
import com.buka.recipe.common.result.ResultGenerator;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import java.io.IOException;

/**
 * @author admin
 */
public class JsonAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
        // 用户未登录
        Result<?> result = ResultGenerator.genFailResult(ResultCode.UNAUTHORIZED.code(), "用户未登录");
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(result));
    }

}
